package com.midian.service.permission.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.dao.SearchHelper.Type;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.model.permission.Auth;
import com.midian.model.permission.Role;
import com.midian.model.permission.RoleAuth;
import com.midian.service.common.BaseService;
import com.midian.service.permission.IRoleService;

/**
 * 角色业务接口实现类
 * @author Mengfh
 *
 */
@Service("roleService")
public class RoleServiceImpl extends BaseService implements IRoleService {
	/**角色DAO*/
	private BaseDaoTemplet<Role> roleDAO;
	/**角色权限DAO*/
	private BaseDaoTemplet<RoleAuth> roleAuthDAO;

	@Resource(name = "roleDAO")
	public void setRoleDAO(BaseDaoTemplet<Role> roleDAO) {
		this.roleDAO = roleDAO;
	}
	
	@Resource(name = "roleAuthDAO")
	public void setRoleAuthDAO(BaseDaoTemplet<RoleAuth> roleAuthDAO) {
		this.roleAuthDAO = roleAuthDAO;
	}
	
	/**
	 * 设置角色权限
	 * @param role 角色对象
	 */
	private void opRoleAuth(Role role) {
		List<String> authIdList = role.getAuthIdList();
		if(authIdList != null && !authIdList.isEmpty()) {
			//角色对象
			Role targetRole = new Role();
			targetRole.setId(role.getId()); //设置角色id
			for (String authId : authIdList) {
				Auth auth = new Auth(); //权限对象
				auth.setId(authId); //设置权限id
				//创建角色权限对象
				RoleAuth roleAuth = new RoleAuth();
				roleAuth.setRole(targetRole); //设置角色对象
				roleAuth.setAuth(auth); //设置权限对象
				roleAuthDAO.add(roleAuth);
			}
		}
	}

	@Override
	public Role add(Role target) throws BizException {
		this.roleDAO.add(target);
		//设置角色权限
		this.opRoleAuth(target);
		return target;
	}

	@Override
	public void update(Role target) throws BizException {
		this.roleDAO.update(target);
		//先清除原来角色所绑定的权限
		String hql = "delete from RoleAuth o where o.role.id = ?";
		this.roleAuthDAO.update(hql, new Object[]{target.getId()});
		//设置角色权限
		this.opRoleAuth(target);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.roleDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.roleDAO.delete(ids);
	}

	@Override
	public Role get(Serializable id) throws BizException {
		return this.roleDAO.get(id);
	}

	@Override
	public List<Role> showList(Role target, Pager pager,
			List<OrderHelper> helper) throws BizException {
		return this.roleDAO.find(target, getSearchHelper(), helper, pager);
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.roleDAO.opDelFlag(ids, status);
	}
	
	/**
	 * 根据角色id获取角色的权限列表
	 * @param roleId 角色id
	 * @return
	 * @throws BizException
	 */
	@Override
	public List<String> getAuthIdListByRoleId(String roleId) throws BizException {
		String hql = "select o.auth.id from RoleAuth o where o.role.id = ?";
		return this.roleAuthDAO.find(hql, new Object[]{roleId});
	}

	@Override
	protected List<SearchHelper> getSearchHelper() {
		String[] attrs = new String[]{"createTime", "name", "delFlag"};
		Type[] types = new Type[]{Type.BETWEEN, Type.BEGINLIKE, Type.EQ};
		return SearchHelper.getSearchList(attrs, types);
	}
}
